package com.pzb.rabbitmq;

import com.pzb.common.enums.QueueEnum;
import com.pzb.common.utils.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
public class RabbitMqSender {
    private static Logger LOGGER = LoggerFactory.getLogger(RabbitMqSender.class);
    @Autowired
    private AmqpTemplate amqpTemplate;

    public void sendMessage(RabbitMqModel mqModel,final long delayTimes){
        //给延迟队列发送消息
        amqpTemplate.convertAndSend(QueueEnum.QUEUE_ORDER_PLUGIN_CANCEL.getExchange(), QueueEnum.QUEUE_ORDER_PLUGIN_CANCEL.getRouteKey(), mqModel, new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                //给消息设置延迟毫秒值
                message.getMessageProperties().setHeader("x-delay",delayTimes);
                return message;
            }
        });
        LOGGER.info("send delay message orderNo:{}",mqModel.getType() +"--"+mqModel.getOrderNo() );
        System.err.println("=================>发送到订单取消队列："+mqModel.getOrderNo()+"--"+ DateUtils.dateTimeNow("yyyy-MM-dd HH:mm:ss"));
    }

    public  void main(String[] args) {
        RabbitMqModel mqModel = new RabbitMqModel();
        mqModel.setType(1);
        mqModel.setOrderNo("测试消息");
        sendMessage(mqModel,10000);
    }

}